﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bruce.Net;
using Game.Foundation;

namespace Game.Client
{
    class MajorConnection    
    {
        private AsyncWorkSocket _connection;

        public event EventHandler<GameListEventArgs> ReceivedGameList;

        public MajorConnection(AsyncWorkSocket connection)
        {
            this._connection = connection;
            this._connection.PacketReceived += new EventHandler<PacketEventArgs>(_connection_PacketReceived);
        }

        //public void Login(int areaId, int roomId, string username)
        //{
        //    GamePacket p = new GamePacket(PacketType.进入聊天室请求);
        //    p.Body.AddRange(BitConverter.GetBytes(areaId));
        //    p.Body.AddRange(BitConverter.GetBytes(roomId));
        //    p.Body.AddRange(BytesParser.StringToBytes(username));
        //    this._connection.Send(p);
        //}

        public void SendGameListRequest()
        {
            Packet p = new Packet((int)PacketType.游戏列表请求包);

            this._connection.Send(p);
        }

        //public void JoinGameGroup(int areaId, int roomId, int deskId, byte index)
        //{
        //    GamePacket p = new GamePacket(PacketType.加入游戏分组请求);
        //    p.Body.AddRange(BitConverter.GetBytes(areaId));
        //    p.Body.AddRange(BitConverter.GetBytes(roomId));
        //    p.Body.AddRange(BitConverter.GetBytes(deskId));
        //    p.Body.Add(index);

        //    this._connection.Send(p);
        //}

        //public void DoReadyOperation(GameStatus operation)
        //{
        //    GamePacket p = new GamePacket(PacketType.准备游戏操作请求);
        //    p.OptionOne = (byte)operation;

        //    this._connection.Send(p);
        //}

        //public void MoveChessman(int xOld, int yOld, int xNew, int yNew)
        //{
        //    GamePacket p = new GamePacket(PacketType.游戏操作请求包);
        //    p.Body.AddRange(BitConverter.GetBytes(xOld));
        //    p.Body.AddRange(BitConverter.GetBytes(yOld));
        //    p.Body.AddRange(BitConverter.GetBytes(xNew));
        //    p.Body.AddRange(BitConverter.GetBytes(yNew));

        //    this._connection.Send(p);
        //}

        void _connection_PacketReceived(object sender, PacketEventArgs e)
        {
            Packet p = e.Packet as Packet;
            //this.DoActionCrossThreads(() => txtMessageHistory.Text += p.Type.ToString() + Environment.NewLine);
            PacketType type = (PacketType)p.Protocol;

            switch (type)
            {
                case PacketType.游戏列表返回包:
                    ProcessGameList(p);
                    break;
            }
        }

        private void ProcessGameList(Packet p)
        {
            if (this.ReceivedGameList == null) return;

            GameListEventArgs e = new GameListEventArgs();
            int gameCount = p.ReadInt32();

            for (int g = 0; g < gameCount; g++)
            {
                GameConfig game = new GameConfig();
                game.Name = p.ReadString();
                game.IPAddress = p.ReadString();
                game.Port = p.ReadInt32();
                game.SeatCount = p.ReadInt32();
                game.RelativePath = p.ReadString();
                game.DownloadPath = p.ReadString();

                int areaCount = p.ReadInt32();
                for (int i = 0; i < areaCount; i++)
                {
                    GameAreaConfig area = new GameAreaConfig();
                    area.ID = p.ReadInt32();
                    area.Name = p.ReadString();
                    int roomCount = p.ReadInt32();

                    for (int j = 0; j < roomCount; j++)
                    {
                        GameRoomConfig room = new GameRoomConfig();
                        room.ID = p.ReadInt32();
                        room.Name = p.ReadString();

                        area.RoomList.Add(room);
                    }

                    game.AreaList.Add(area);
                }
                e.GameList.Add(game);
            }

            this.ReceivedGameList(this, e);
        }
    }
}
